package com.javatian.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javatian.common.ResultCode;
import com.javatian.dao.AdminInfoDao;
import com.javatian.entity.AdminInfo;
import com.javatian.service.AdminInfoService;
import exception.CustomException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员对外提供的服务的实现类
 */
@Service("adminInfoService")
public class AdminInfoServiceImpl implements AdminInfoService {

    @Resource
    private AdminInfoDao adminInfoDao;
    /**
     * 根据姓名精确查询一条数据
     */
    @Override
    public AdminInfo findByUserName(String username) {

        return adminInfoDao.findByUserName(username);
    }

    /**
     * 根据用户名和密码判断用户是否存在，如果存在，返回这个用户；否者返回错误信息
     */
    @Override
    public AdminInfo login(String name, String password) {
        AdminInfo adminInfo = adminInfoDao.findByUserName(name);
        if (adminInfo==null){
            throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
        }
        if (!SecureUtil.md5(password).equalsIgnoreCase((adminInfo.getPassword()))){
            throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
        }
        return adminInfo;
    }

    /**
     * 增加一条记录
     *
     * @param adminInfo
     */
    @Override
    public AdminInfo add(AdminInfo adminInfo) {
        //唯一校验
        int count = adminInfoDao.checkRepeat("name", adminInfo.getName(), null);
        if (count > 0){
            throw new CustomException("2001","用户名\""+adminInfo.getName()+"\"已存在");
        }
        if (StringUtil.isEmpty(adminInfo.getPassword())){
            //默认密码
            adminInfo.setPassword(SecureUtil.md5("123456"));
        }else {
            adminInfo.setPassword(SecureUtil.md5(adminInfo.getPassword()));
        }
        adminInfoDao.insertSelective(adminInfo);
        return adminInfo;
    }

    /**
     * 删除一条记录
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        adminInfoDao.deleteByPrimaryKey(id);
    }

    /**
     * 修改一条记录
     *
     * @param adminInfo
     */
    @Override
    public void update(AdminInfo adminInfo) {
        adminInfoDao.updateByPrimaryKeySelective(adminInfo);
    }

    /**
     * 根据主键查询一条记录
     *
     * @param id
     */
    @Override
    public AdminInfo findById(Long id) {
        return adminInfoDao.selectByPrimaryKey(id);
    }

    /**
     * 查询所有管理员记录
     */
    @Override
    public List<AdminInfo> findAll() {
        return adminInfoDao.findByName("all");
    }

    /**
     * 根据条件分页查询
     *
     * @param name     用户名
     * @param pageNum  第几页
     * @param pageSize 每页数量
     * @return 当前页的List列表
     */
    @Override
    public PageInfo<AdminInfo> findPage(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<AdminInfo> all = adminInfoDao.findByName(name);
        return PageInfo.of(all);
    }

    /**
     * 管理员总数
     */
    @Override
    public Integer count() {
        return adminInfoDao.count();
    }
}
